package com.ecube.maintenance.biz.apis.impl;

import android.content.Context;

import com.ecube.maintenance.biz.apis.ICarBrandAction;
import com.ecube.maintenance.biz.commons.IListItem;
import com.ecube.maintenance.pojos.CarPropertyListInfo;
import com.ecube.maintenance.utils.GsonUtil;
import com.google.gson.reflect.TypeToken;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Trible Chen on 2014/9/17.
 */
public class CarBrandAction {

    static ProxyCarBrandAction proxyCityAction;
    public static boolean isInitCarInfos = false;

    private static List<CarPropertyListInfo> getStaticCityList(Context context){
        if ( proxyCityAction == null ){
            proxyCityAction = new ProxyCarBrandAction(context);
            return proxyCityAction.getStaticCarBrandList();
        }
        return proxyCityAction.carBrandListInfos;
    }

    public static List<IListItem<CarPropertyListInfo.BaseCarPropertyList.BaseCarProperty>> getCarBrands(Context context){
        List<CarPropertyListInfo> all = getStaticCityList(context);
        if ( all == null )return null;
        for ( CarPropertyListInfo car : all ){
            if( "brand".equals(car.getType())){
                List<CarPropertyListInfo.BaseCarPropertyList> content
                        = car.getContent();
                return resolveContent(content);
            }
        }
        return null;
    }
    public static List<IListItem<CarPropertyListInfo.BaseCarPropertyList.BaseCarProperty>> getCarSerialsByBrandId(int bid){
        List<CarPropertyListInfo> all = proxyCityAction.carBrandListInfos;
        if ( all == null )return null;
        for ( CarPropertyListInfo car : all ){
            if( "serial".equals(car.getType()) && car.getBrandid() == bid ){
                List<CarPropertyListInfo.BaseCarPropertyList> content
                        = car.getContent();
                return resolveContent(content);
            }
        }
        return null;
    }

    public static List<IListItem<
            CarPropertyListInfo.BaseCarPropertyList.BaseCarProperty>> getCarModelsByBrandIdAndSerialId(int bid,int sid){
        List<CarPropertyListInfo> all = proxyCityAction.carBrandListInfos;
        if ( all == null )return null;
        for ( CarPropertyListInfo car : all ){
            if( "model".equals(car.getType())
                    && car.getSerialID() == sid
                    && car.getBrandid() == bid ){
                List<CarPropertyListInfo.BaseCarPropertyList> content
                        = car.getContent();
                return resolveContent(content);
            }
        }
        return null;
    }

    private static List<
            IListItem<CarPropertyListInfo.BaseCarPropertyList.BaseCarProperty>
            > resolveContent(List<CarPropertyListInfo.BaseCarPropertyList> content){

        List<IListItem<CarPropertyListInfo.BaseCarPropertyList.BaseCarProperty>> result
                = new ArrayList<IListItem<CarPropertyListInfo.BaseCarPropertyList.BaseCarProperty>>();
        if ( content == null )return null;
        for (CarPropertyListInfo.BaseCarPropertyList bpl : content ){
            result.add(bpl);
            List<CarPropertyListInfo.BaseCarPropertyList.BaseCarProperty> bp =
                    bpl.getData();
            for (CarPropertyListInfo.BaseCarPropertyList.BaseCarProperty temp : bp ){
                result.add(temp);
            }
        }
        return result;
    }


    public static class ProxyCarBrandAction implements ICarBrandAction{
        List<CarPropertyListInfo> carBrandListInfos;
        Context mContext;
        public ProxyCarBrandAction(Context context) {
            mContext = context;
        }

        @Override
        public List<CarPropertyListInfo> getStaticCarBrandList() {
            try {
                String s = IOUtils.toString(mContext.getAssets().open("carOneline.json"));
                TypeToken<List<CarPropertyListInfo>> citytoken = new TypeToken<List<CarPropertyListInfo>>(){};
                carBrandListInfos = GsonUtil.getInfosFromJson(s,citytoken.getType());
                isInitCarInfos = true;
                return carBrandListInfos;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}
